package com.resou.reader.reader.subsidy;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.entry.SubSidyDetailBean;
import com.resou.reader.api.service.SubSidyService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.PreferenceUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 补贴模式P层
 */
public class SubSidyPresenter extends ResouBasePresenter<ISubSidyContract.ISubSidyView> implements ISubSidyContract.ISubSidyPresenter {

    SubSidyService subSidyService;

    public SubSidyPresenter(ISubSidyContract.ISubSidyView view) {
        super(view);
        subSidyService = ApiImp.getInstance().getService(SubSidyService.class);
    }

    /***
     *获取补贴模式列表
     */
    @Override
    public void quearSubSidyList() {
        mView.showProgress();
        subSidyService.quearSubSidyDetail(UserInstance.getToken())
                .observeOn(getMainSchedulers())
                .subscribeOn(getIOSchedulers())
                .subscribe(new Observer<Result<SubSidyDetailBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<SubSidyDetailBean> subSidyDetailBeanResult) {
                        mView.showData(subSidyDetailBeanResult.getData());
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });
    }

    /***
     *设置阅读模式
     * @param subSidyType 阅读模式   0.，省钱（有广告），1 vip （无广告）2 普通（正常购买模式）
     */
    @Override
    public void setSubSidy(int subSidyType) {
        subSidyService.setSubSidyModel(UserInstance.getToken(), subSidyType)
                .observeOn(getMainSchedulers())
                .subscribeOn(getIOSchedulers())
                .subscribe(new Observer<Result<SubSidyDetailBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result<SubSidyDetailBean> subSidyDetailBeanResult) {
//                        mView.showData(subSidyDetailBeanResult.getData());
                        PreferenceUtil.getInstance(mView.getContext()).saveSubSidyStateValue(subSidyType);
                        mView.setSubSidySucess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                        mView.setSubSidyFail();
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }
}
